package com.khshop.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.khshop.pojo.Menupriv;
import com.khshop.pojo.Role;
import com.khshop.service.MenuprivService;
import com.khshop.service.RoleService;
import com.khshop.vo.AjaxResult;
import com.khshop.vo.ServiceResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author zhaoyang
 * @since 2020-10-16
 */
@Api(tags = "角色表 前端控制器")
@RestController
@RequestMapping("/")
public class RoleController {

    @Autowired
    RoleService roleService;

    //新增权限角色
    @ApiOperation("新增权限角色")
    @PostMapping("/addMRole")
    @ResponseBody
    public AjaxResult addRole(@RequestBody Role role) {
        if (roleService.addRole(role).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //修改权限角色
    @ApiOperation("修改权限角色")
    @PostMapping("/updateRole")
    @ResponseBody
    public AjaxResult updateRole(@RequestBody Role role) {
        if (roleService.updateRole(role).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //删除权限角色
    @ApiOperation("删除权限角色")
    @PostMapping("/delRole")
    @ResponseBody
    public AjaxResult delRole(Integer id) {
        if (roleService.delRole(id).isFlag()) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }

    }

    //查询权限角色
    @ApiOperation("查询权限角色")
    @PostMapping("/searchRole")
    @ResponseBody
    public AjaxResult searchRole(@RequestBody Map<String, String> map,
                                     @RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "limit", defaultValue = "15") Integer limit) {

        ServiceResult<IPage<Role>> serviceResult = roleService.searchRole(map, page, limit);
        if (serviceResult.isFlag()) {
            return AjaxResult.success((int) serviceResult.getData().getTotal(), serviceResult.getData());
        } else {
            return AjaxResult.error();
        }

    }
}

